﻿

local moduleName = "Agro"



----------------------------
--      Localization      --
----------------------------

local L = {
	[moduleName] = "Agro Monitor",

	["Check frequency"] = true,
	["Frequency in seconds."] = true,

	["Sound Warning"] = true,
	["Play a sound when you get agro in these area's."] = true,

	["Disable"] = true,
	["Everywhere"] = true,
	["Party,Raid,Outdoors"] = true,
	["Party,Raid,Outdoors,Arena"] = true
}
if GetLocale() == "zhCN" then
	L[moduleName] = "仇恨监视器"

	L["Check frequency"] = "更新频率"
	L["Frequency in seconds."] = "单位:秒"

	L["Sound Warning"] = "音效提示"
	L["Play a sound when you get agro in these area's."] = "在哪些情况下OT后播放音效"

	L["Disable"] = "禁用"
	L["Everywhere"] = "任何时候"
	L["Party,Raid,Outdoors"] = "小队,团队,外域"
	L["Party,Raid,Outdoors,Arena"] = "小队,团队,外域,竞技场"
elseif GetLocale() == "zhTW" then
	L[moduleName] = "仇恨監視器"

	L["Check frequency"] = "更新頻率"
	L["Frequency in seconds."] = "單位:秒"

	L["Sound Warning"] = "音效提示"
	L["Play a sound when you get agro in these area's."] = "在哪些情況下OT後播放音效"

	L["Disable"] = "禁用"
	L["Everywhere"] = "任何時候"
	L["Party,Raid,Outdoors"] = "小隊,團隊,外域"
	L["Party,Raid,Outdoors,Arena"] = "小隊,團隊,外域,競技場"
end
----------------------------------
--      Local Declaration      --
----------------------------------

local lookup = { } -- table for name to unit id lookups
local myunit
local playsound = false

local where = {
	L["Disable"],
	L["Everywhere"],
	L["Party,Raid,Outdoors"],
	L["Party,Raid,Outdoors,Arena"]
}

local sound = "Sound\\Creature\\Murloc\\mMurlocAggroOld.wav"
-- "Sound\\Spells\\PVPFlagTakenHorde.wav"


-- localize these functions to speed up the main loop a bit
local UnitExists = UnitExists
local UnitIsEnemy = UnitIsEnemy
local UnitIsUnit = UnitIsUnit



----------------------------------
--      Module Declaration      --
----------------------------------

local plugin = BunchOfBars:NewModule(moduleName, "AceTimer-3.0")

plugin.revision = tonumber(("$Revision: 40 $"):match("%d+"))

plugin.options = {
	name = L[moduleName],
	args = {
		frequency = {
			type = "range",
			name = L["Check frequency"],
			desc = L["Frequency in seconds."],
			min = 0.1,
		        max = 2.0,
		        step = 0.05,
		        get = "GetSetFrequency",
		        set = "GetSetFrequency",
		},
		sound = {
			type     = "select",
			name     = L["Sound Warning"],
			desc     = L["Play a sound when you get agro in these area's."],
			values = where,
 			get      = "GetSetSound",
            		set      = "GetSetSound"
		},
	}
}

plugin.defaultDB = {
	profile = {
		frequency = 0.25,
		sound     = 4 -- Party,Raid,Outdoors,Arena
	}
}


plugin.agro = {} -- can't be local since other modules need access



----------------------------------
--      Module Functions        --
----------------------------------

function plugin:OnEnable()
	self:ScheduleRepeatingTimer(self.Update, self.db.profile.frequency, self)
	self:RegisterEvent("ZONE_CHANGED_NEW_AREA")
end


function plugin:OnUpdate(frame)
	lookup[UnitName(frame.unit)] = frame.unit

	if UnitIsUnit(frame.unit, "player") then
		myunit = frame.unit
	end
end

function plugin:Update()
	local tanking = self.agro[myunit]
	local threat, exists

	for unit in pairs(self.core.frames.core) do
		self.agro[unit] = 0
	end

	for unit in pairs(self.core.frames.core) do
		if UnitExists(unit.."target") and UnitCanAttack(unit.."target", "player") then
			exists = UnitExists(unit.."targettarget")

			if playsound and exists and (GetCVar("Sound_EnableSFX") == 1) and UnitIsUnit(unit.."targettarget", "player") then
				PlaySoundFile(sound)
			end

			if UnitPlayerControlled(unit.."target") then
				if exists then
					local tunit = lookup[UnitName(unit.."targettarget")]
					if tunit then -- check if the person is in out party/raid
						self.agro[tunit] = 3
						--self.agrounit[tunit][unit.."target"] = true

						self:SendMessage("BunchOfBars_Agro", tunit)
					end
				end
			else
				for tunit in pairs(self.core.frames.core) do
					threat = UnitThreatSituation(tunit, unit.."target")

					if threat then
						self.agro[tunit] = max(self.agro[tunit], threat)

						--[[if (threat > 1) then
							self.agrounit[tunit][unit.."target"] = true
						end]]--
					end
				end
			end
		end
	end

	for unit in pairs(self.core.frames.core) do
		if self.agro[unit] then
			self:SendMessage("BunchOfBars_Agro", unit)
		end
	end

	--[[for unit in pairs(self.agro) do
		self.agro[unit] = self.agro[unit] - self.db.profile.frequency

		if self.agro[unit] < 0 then
			self.agro[unit] = nil
		end

		self:SendMessage("BunchOfBars_Agro", unit)
	end]]--
end


function plugin:OnInactive(frame)
	self.agro[frame.unit] = nil
end


function plugin:ZONE_CHANGED_NEW_AREA()
	local _, it = IsInInstance()

	if (self.db.profile.sound == 2) or -- Everywhere
	   (self.db.profile.sound == 3 and it ~= "pvp" and it ~= "arena") or -- Party,Raid,Outdoors
	   (self.db.profile.sound == 4 and it ~= "pvp") then -- Party,Raid,Outdoors,Arena
		playsound = true
	else
		playsound = false
	end
end



----------------------------------
--      Option Handlers         --
----------------------------------

function plugin:GetSetFrequency(_, v)
	if type(v) == "nil" then return self.db.profile.frequency end

	-- this check is to assure we don't reschedule the event to often
	if self.db.profile.frequency ~= v then
		self.db.profile.frequency = v

		self:OnEnable()
	end
end


function plugin:GetSetSound(_, v)
	if type(v) == "nil" then return self.db.profile.sound end

	self.db.profile.sound = v

	self:ZONE_CHANGED_NEW_AREA()

	if self.db.profile.sound > 1 then
		PlaySoundFile(sound)
	end
end
